package com.zg.autoflow.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.autoflow.req.AutoflowMainAddReq;
import com.zg.autoflow.req.AutoflowMainAllReq;
import com.zg.autoflow.req.AutoflowMainIdReq;
import com.zg.autoflow.req.AutoflowMainPageReq;
import com.zg.autoflow.service.AutoflowMainService;
import com.zg.autoflow.vo.AutoFlowMainLinkVo;
import com.zg.autoflow.vo.AutoflowMainPageVo;
import com.zg.autoflow.vo.AutoflowMainUpdateVo;
import com.zg.common.core.R;
import com.zg.common.core.dao.BaseEntity;
import com.zg.common.core.dao.autoflow.dao.AutoflowMainDao;
import com.zg.common.core.dao.autoflow.entity.AutoflowMain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
* @author zg
*/
@RestController
@RequiredArgsConstructor
@Api(tags = {"流程配置初始化"})
public class AutoflowMainController {
	private final AutoflowMainDao autoflowMainDao;
	private final AutoflowMainService autoflowMainService;

	@ApiOperation("新增流程")
	@PostMapping("/autoFlow/main/add")
	public R<?> add(@RequestBody @Valid AutoflowMainAddReq param) {
		AutoflowMain obj = autoflowMainService.add(param);
		return R.success(obj);
	}
	@ApiOperation("删除流程")
	@PostMapping("/autoFlow/main/del")
	public R<?> del(@RequestBody List<Long> idList) {
		autoflowMainService.del(idList);
		return R.success();
	}
	@ApiOperation("修改流程")
	@PostMapping("/autoFlow/main/update")
	public R<?> update(@RequestBody @Valid AutoflowMainUpdateVo param) {
		AutoflowMain obj = autoflowMainService.update(param);
		return R.success(obj);
	}


	@ApiOperation("获取流程信息")
	@PostMapping("/autoFlow/main/info")
	public R<AutoflowMain> info(Long id) {
		AutoflowMain obj = autoflowMainDao.getById(id);
		return R.success(obj);
	}
	@ApiOperation("获取流程信息")
	@PostMapping("/autoFlow/main/all")
	public R<List<AutoflowMain>> all(@RequestBody AutoflowMainAllReq flowCode) {
		LambdaQueryWrapper<AutoflowMain> eq = new LambdaQueryWrapper<AutoflowMain>().eq(BaseEntity::getIsDel, false);
		if (StringUtils.isNotEmpty(flowCode.getUnFlowCode())){
			eq.ne(AutoflowMain::getCode,flowCode.getUnFlowCode());
		}
		if (StringUtils.isNotEmpty(flowCode.getFlowCode())){
			eq.eq(AutoflowMain::getCode,flowCode.getFlowCode());
		}
		if (StringUtils.isNotEmpty(flowCode.getFlowName())){
			eq.like(AutoflowMain::getName,flowCode.getFlowName());
		}
		eq.orderByDesc(BaseEntity::getCreateTime);
		List<AutoflowMain> list = autoflowMainDao.list(eq);
		return R.success(list);
	}
	@ApiOperation("分页获取流程信息")
	@PostMapping("/autoFlow/main/page")
	public R<IPage<AutoflowMainPageVo>> page(@RequestBody @Valid AutoflowMainPageReq autoflowMainPageReq) {

		IPage<AutoflowMainPageVo> pb = autoflowMainService.page(new Page<AutoflowMainPageVo>(autoflowMainPageReq.getPage(), autoflowMainPageReq.getPageSize()), autoflowMainPageReq);
		return R.success(pb);
	}

	/**
	 * excel导入
	 * @param ID
	 * @param file
	 * @return
	 * @throws Exception
	 */
	@ApiOperation("excel导入流程")
	@PostMapping("/autoFlow/main/saveOrUpdateAutoFlow")
	public R<?> saveOrUpdateAutoFlow(@RequestParam(name = "id",required = false) String ID, @RequestParam(name = "file") MultipartFile file) throws Exception {
		autoflowMainService.insetOrUpdateAutoFlow(ID, file);
		return R.success("操作成功");
	}

	/**
	 * 导出流程环节
	 */
	@ApiOperation("excel导出流程信息")
	@GetMapping( path = "/autoFlow/main/exportFlowExcel")
	public void getExportProcessLink(HttpServletResponse response, @RequestParam(name = "flowId") Long flowId) throws Exception {
		autoflowMainService.exportFlowExcel(response, flowId);
	}

	@PostMapping(path = "/autoFlow/main/syncAutoFlow")
	@ApiOperation("流程发布")
	public R<?> syncAutoFlow(@RequestBody @Valid AutoflowMainIdReq param) {
		Long flowId = param.getFlowId();
		autoflowMainService.syncAutoFlow(flowId);
		return R.success();
	}
	@PostMapping(path = "/autoFlow/main/agreeAutoFlow")
	@ApiOperation("审核通过")
	public R<?> agreeAutoFlow(@RequestBody @Valid AutoflowMainIdReq param) {
		String r = autoflowMainService.agreeAutoFlow(param);
		return R.success(r);
	}


	@PostMapping(path = "/autoFlow/main/unagreeAutoFlow")
	@ApiOperation("审核未通过")
	public R<?> unagreeAutoFlow(@RequestBody AutoflowMainIdReq param) {
		String  r  = autoflowMainService.unagreeAutoFlow(param);
		return R.success(r);
	}

	@PostMapping(path = "/autoFlow/main/getAutoFlowMainLink")
	@ApiOperation("获取全链路流程环节信息")
	public R<AutoFlowMainLinkVo> getAutoFlowMainLink(@RequestBody @Valid AutoflowMainIdReq param) {
		AutoFlowMainLinkVo autoFlowMainLinkVo = autoflowMainService.getAutoFlowMainLink(param.getFlowId());
		return R.success(autoFlowMainLinkVo);
	}
}
